package com.cloud.mall.oms.service.impl;

import com.cloud.mall.api.CommonPage;
import com.cloud.mall.base.BaseResponse;
import com.cloud.mall.core.bean.MallBeanUtils;
import com.cloud.mall.oms.dao.OmsOrderReturnApplyDao;
import com.cloud.mall.oms.dto.OmsReturnApplyQueryParam;
import com.cloud.mall.oms.dto.OmsUpdateStatusParam;
import com.cloud.mall.oms.mapper.OmsOrderReturnApplyMapper;
import com.cloud.mall.oms.model.OmsOrderReturnApply;
import com.cloud.mall.oms.model.OmsOrderReturnApplyExample;
import com.cloud.mall.oms.out.OmsOrderReturnApplyOut;
import com.cloud.mall.oms.service.OmsOrderReturnApplyService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class OmsOrderReturnApplyServiceImpl implements OmsOrderReturnApplyService {
    @Autowired
    private OmsOrderReturnApplyDao returnApplyDao;
    @Autowired
    private OmsOrderReturnApplyMapper returnApplyMapper;

    @Override
    public BaseResponse<CommonPage<OmsOrderReturnApplyOut>> list(OmsReturnApplyQueryParam queryParam, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<OmsOrderReturnApply> returnApplyList=returnApplyDao.getList(queryParam);
        return BaseResponse.setResultSuccess(CommonPage.restPage(MallBeanUtils.copyDoToDto(returnApplyList,OmsOrderReturnApplyOut.class)));
    }

    @Override
    public BaseResponse delete(List<Long> ids) {

        OmsOrderReturnApplyExample example = new OmsOrderReturnApplyExample();
        example.createCriteria().andIdIn(ids).andStatusEqualTo(3);
        return BaseResponse.setResultSuccess(returnApplyMapper.deleteByExample(example));
    }

    @Override
    public BaseResponse<OmsOrderReturnApplyOut> getItem(Long id) {
        return BaseResponse.setResultSuccess(MallBeanUtils.doToDto(returnApplyMapper.selectByPrimaryKey(id),OmsOrderReturnApplyOut.class));
    }

    @Override
    public BaseResponse updateStatus(Long id, OmsUpdateStatusParam statusParam) {
        Integer status = statusParam.getStatus();
        OmsOrderReturnApply returnApply = new OmsOrderReturnApply();
        if(status.equals(1)){
            //确认退货
            returnApply.setId(id);
            returnApply.setStatus(1);
            returnApply.setReturnAmount(statusParam.getReturnAmount());
            returnApply.setCompanyAddressId(statusParam.getCompanyAddressId());
            returnApply.setHandleTime(new Date());
            returnApply.setHandleMan(statusParam.getHandleMan());
            returnApply.setHandleNote(statusParam.getHandleNote());
        }else if(status.equals(2)){
            //完成退货
            returnApply.setId(id);
            returnApply.setStatus(2);
            returnApply.setReceiveTime(new Date());
            returnApply.setReceiveMan(statusParam.getReceiveMan());
            returnApply.setReceiveNote(statusParam.getReceiveNote());
        }else if(status.equals(3)){
            //拒绝退货
            returnApply.setId(id);
            returnApply.setStatus(3);
            returnApply.setHandleTime(new Date());
            returnApply.setHandleMan(statusParam.getHandleMan());
            returnApply.setHandleNote(statusParam.getHandleNote());
        }else{
            return BaseResponse.setResultSuccess(0);
        }
       return BaseResponse.setResultSuccess(returnApplyMapper.updateByPrimaryKeySelective(returnApply));
    }
}
